import AutomaticRecode from "../../Commons/AutomaticRecode";
import { CharacterType, SkillIdCenter } from "../../Enums/GameEnum";
import SaveManager from "../../Managers/SaveManager";
import EquipManager from "../Equips/EquipManager";
import LevelManager from "../Levels/LevelManager";
import MapManager from "../Maps/MapManager";
import PetManager from "../Pets/PetManager";
import SkillManager from "../Skills/SkillManager";
import CharacterManager from "./CharacterManager";
import CharacterTemplate from "./CharacterTemplate";

export default class CharacterEntity extends AutomaticRecode {
    public id: number;
    public lv: number;

    constructor(value: any) {
        super();
        this.init(value);
    }
    public get combatTime(): number {
        var num = this.characterTp.combatTime;
        if (SkillManager.single.isUnlockSkill(SkillIdCenter.id_1008)) {
            var ey = SkillManager.single.getSkillEntity(SkillIdCenter.id_1008);
            var skillvalue = ey.valueNum > 50 ? 50 : ey.valueNum;
            num *= (1 - skillvalue * 0.01);
        }

        return Math.ceil(num);
    }

    public get characterTp(): CharacterTemplate {
        return CharacterManager.single.getCharacterTemplate(this.id);
    }
    public get attackGrow(): number {
        var num = this.characterTp.attackGrow;
        if (SkillManager.single.isUnlockSkill(SkillIdCenter.id_1010)) {
            var ey = SkillManager.single.getSkillEntity(SkillIdCenter.id_1010);
            num += ey.valueNum * 0.01;
        }
        return num;
    }

    public get attack(): number {
        var num = this.lv * this.attackGrow + this.characterTp.attack;
        if (this.characterTp.unitType == 3) {
            num *= 1.1;
        }
        if (this.characterTp.characterType == CharacterType.player) {
            if (SkillManager.single.isUnlockSkill(SkillIdCenter.id_1002)) {
                var ey = SkillManager.single.getSkillEntity(SkillIdCenter.id_1002);
                num *= (1 + ey.valueNum * 0.01);
            }
            var equipValue = EquipManager.single.getEquipAttribute(104);
            num += equipValue;

            var petAttributeNum = PetManager.single.getPetAttribute(104);
            num *= (1 + petAttributeNum * 0.01);
        }

        return Math.ceil(num);
    }
    public get healthGrow(): number {
        var num = this.characterTp.healthGrow;
        if (SkillManager.single.isUnlockSkill(SkillIdCenter.id_1010)) {
            var ey = SkillManager.single.getSkillEntity(SkillIdCenter.id_1010);
            num += ey.valueNum;
        }
        return num;
    }

    public get health(): number {
        var num = this.lv * this.healthGrow + this.characterTp.health;
        if (this.characterTp.characterType == CharacterType.player) {
            if (SkillManager.single.isUnlockSkill(SkillIdCenter.id_1001)) {
                var ey = SkillManager.single.getSkillEntity(SkillIdCenter.id_1001);
                num *= (1 + ey.valueNum);
            }
            var equipValue = EquipManager.single.getEquipAttribute(101);
            num += equipValue;

            var petAttributeNum = PetManager.single.getPetAttribute(101);
            num *= (1 + petAttributeNum * 0.01);
        } else {
            var userdata = SaveManager.single.userdata;
            var mapTp = MapManager.single.getMapTemplate(userdata.currentMapId);
            num *= (1 + mapTp.enemyHpAdd * 0.01);
        }

        return Math.ceil(num);
    }
    public get critical(): number {
        var num = this.characterTp.critical;
        if (this.characterTp.characterType == CharacterType.player) {
            if (SkillManager.single.isUnlockSkill(SkillIdCenter.id_1003)) {
                var ey = SkillManager.single.getSkillEntity(SkillIdCenter.id_1003);
                num += ey.valueNum * 100;
            }
            var equipValue = EquipManager.single.getEquipAttribute(102);
            num += equipValue * 100;

            var petAttributeNum = PetManager.single.getPetAttribute(102);
            num += petAttributeNum * 100;
        }

        return num;
    }
    public get criticalDamage(): number {
        var num = this.characterTp.criticalDamage;
        if (this.characterTp.characterType == CharacterType.player) {
            if (SkillManager.single.isUnlockSkill(SkillIdCenter.id_1004)) {
                var ey = SkillManager.single.getSkillEntity(SkillIdCenter.id_1004);
                num += ey.valueNum;
            }

            var equipValue = EquipManager.single.getEquipAttribute(103);
            num += equipValue;

            var petAttributeNum = PetManager.single.getPetAttribute(103);
            num += petAttributeNum;
        }


        return num;
    }
    public get density(): number {
        var num = 1;
        if (this.characterTp.unitType == 1) {

        } else if (this.characterTp.unitType == 2) {
            num *= 1.2;
        } else if (this.characterTp.unitType == 3) {
            num *= 1.5;
        }
        return num;
    }
    public get moveSpeed(): number {
        var num = this.characterTp.moveSpeed;
        if (this.characterTp.unitType == 1) {
            num *= 1.5;
        } else if (this.characterTp.unitType == 2) {
            num *= 1.2;
        } else if (this.characterTp.unitType == 3) {
            num *= (1 - 0.25);
        }
        if (this.characterTp.characterType == CharacterType.player) {
            if (SkillManager.single.isUnlockSkill(SkillIdCenter.id_1005)) {
                var ey = SkillManager.single.getSkillEntity(SkillIdCenter.id_1005);
                num *= (1 + ey.valueNum * 0.01);
            }
            var equipValue = EquipManager.single.getEquipAttribute(105);
            num *= (1 + equipValue * 0.01);

            var petAttributeNum = PetManager.single.getPetAttribute(105);
            num *= (1 + petAttributeNum * 0.01);
        }
        return num;
    }

    public get gold(): number {
        var num = this.characterTp.reward + this.lv * 20;
        if (SkillManager.single.isUnlockSkill(SkillIdCenter.id_1006)) {
            var ey = SkillManager.single.getSkillEntity(SkillIdCenter.id_1006);
            num *= (1 + ey.valueNum * 0.01);
        }
        return Math.floor(num);
    }

    public get upgradeCost(): number {
        var lvTp = LevelManager.single.getLevelTemplate(this.lv);
        var num = lvTp.cost;
        if (SkillManager.single.isUnlockSkill(SkillIdCenter.id_1009)) {
            var ey = SkillManager.single.getSkillEntity(SkillIdCenter.id_1009);
            num *= (1 - ey.valueNum * 0.01);
        }

        return Math.ceil(num);
    }

}